package com.goldenautumn.prefabricate.activity.mall.adapter

import android.content.Context
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.GoodsSkuVo
import com.goldenautumn.prefabricate.business.entity.GoodsSpecVo
import com.goldenautumn.prefabricate.databinding.ItemGoodsSpecBinding

class GoodsSpecAdapter : BaseQuickAdapter<GoodsSpecVo, DataBindingHolder<ItemGoodsSpecBinding>>() {

    var isGrid = false

    var selectPosition = -1

    override fun onBindViewHolder(holder: DataBindingHolder<ItemGoodsSpecBinding>, position: Int, item: GoodsSpecVo?) {
        if(item == null) return
        var mBinding  = holder.binding

        var params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        if(isGrid) params.setMargins(SizeUtils.dp2px(6f), SizeUtils.dp2px(6f),SizeUtils.dp2px(6f), SizeUtils.dp2px(6f))
        else when (position) {
            0 -> params.setMargins(SizeUtils.dp2px(16f), 0, 0, 0)
            items.size - 1 -> params.setMargins(SizeUtils.dp2px(12f), 0, SizeUtils.dp2px(16f), 0)
            else -> params.setMargins(SizeUtils.dp2px(12f), 0,0, 0)
        }
        holder.itemView.layoutParams = params

        if(selectPosition == position) {
            mBinding.spec.setTextColor(ContextCompat.getColor(context,R.color.color_1D1F20))
            mBinding.spec.shapeBuilder?.setShapeStrokeWidth(SizeUtils.dp2px(2f))?.setShapeStrokeColor(ContextCompat.getColor(context,R.color.colorPrimary))?.into(mBinding.spec)
        } else {
            mBinding.spec.setTextColor(ContextCompat.getColor(context, qncx.external.common.R.color.color_666666))
            mBinding.spec.shapeBuilder?.setShapeStrokeWidth(SizeUtils.dp2px(1f))?.setShapeStrokeColor(ContextCompat.getColor(context,
                qncx.external.common.R.color.color_cccccc))?.into(mBinding.spec)
        }

        mBinding.spec.text = "${item.spec_value}"

        mBinding.spec.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                var lp = mBinding.body.layoutParams
                mBinding.spec.viewTreeObserver.removeOnGlobalLayoutListener(this)
                lp.width = mBinding.spec.width
                mBinding.body.layoutParams = lp
            }
        })

    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemGoodsSpecBinding> {
        return DataBindingHolder(R.layout.item_goods_spec,parent)
    }

}